<?php
/**
 * Clase Base FN para BaseImStockFN
 *
 * @package    
 * @subpackage BaseImStockFN
 * @author     hans
 * @version    SVN:: BaseImStockFN.class.php,v 1.1 2012-03-18 20:29:05 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/BN/ImStockBN.class.php');

class BaseImStockFN extends ImStockBN
{
  private static $fieldNames = array (
    'AlmId'=>'stk_almId', 'ArtId'=>'stk_artId', 'CantFisico'=>'stk_cantFisico', 'CantReserva'=>'stk_cantReserva', 'CantDsipon'=>'stk_cantDispon', 'CostoProm'=>'stk_costoProm', 'CostoUlt'=>'stk_costoUlt', 'ModifUsr'=>'stk_modif_usr', 'ModifFecha'=>'stk_modif_fecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();     
      $sql = "SELECT stk_almId, stk_artId, stk_cantFisico, stk_cantReserva, stk_cantDispon, stk_costoProm, stk_costoUlt, stk_modif_usr, stk_modif_fecha FROM im_stock WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectExtended()
  {
    try
    {
      $where = $this->buildConsult();
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();     
      $sql = "
            SELECT 
                stk_almId, 
                stk_artId, 
                stk_cantFisico, 
                stk_cantReserva, 
                stk_cantDispon, 
                stk_costoProm, 
                stk_costoUlt, 
                stk_modif_usr, 
                stk_modif_fecha, 
                itm_descripcion,
                itm_UoM,
                alm_nombre,
                itm_tipo
            FROM im_stock 
                LEFT JOIN im_articulo ON stk_artId = itm_articuloId
                LEFT JOIN im_almacen ON stk_almId = alm_almacenId
            WHERE 1 ".$where." 
            ORDER BY alm_nombre,itm_tipo,itm_descripcion";
      $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }
  
  
  
  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT stk_almId, stk_artId, stk_cantFisico, stk_cantReserva, stk_cantDispon, stk_costoProm, stk_costoUlt, stk_modif_usr, stk_modif_fecha FROM im_stock WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getAlmId()){ $where .= " AND stk_almid = '".$this->getAlmId()."'"; }
      if($this->getArtId()){ $where .= " AND stk_artid = '".$this->getArtId()."'"; }
      if($this->getItemDescripcion()){ $where .= " AND itm_descripcion LIKE '".$this->getItemDescripcion()."%'"; }      
      if($this->getCantFisico()){ $where .= " AND stk_cantfisico = '".$this->getCantFisico()."'"; }
      if($this->getCantReserva()){ $where .= " AND stk_cantreserva = '".$this->getCantReserva()."'"; }
      if($this->getCantDsipon()){ $where .= " AND stk_cantdsipon = '".$this->getCantDsipon()."'"; }
      if($this->getCostoProm()){ $where .= " AND stk_costoprom = '".$this->getCostoProm()."'"; }
      if($this->getCostoUlt()){ $where .= " AND stk_costoult = '".$this->getCostoUlt()."'"; }
      if($this->getModifUsr()){ $where .= " AND stk_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND stk_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "
            SELECT stk_almId, stk_artId, stk_cantFisico, stk_cantReserva, stk_cantDispon, stk_costoProm, stk_costoUlt, stk_modif_usr, stk_modif_fecha 
            FROM im_stock 
            WHERE 1 
                AND stk_artId='".$this->getArtId()."'
                AND stk_almId='".$this->getAlmId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;     
      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseImStockFN = new ImStockBN();
    $o_BaseImStockFN->setAlmId($o_propel->stk_almId);
    $o_BaseImStockFN->setArtId($o_propel->stk_artId);
    $o_BaseImStockFN->setCantFisico($o_propel->stk_cantFisico);
    $o_BaseImStockFN->setCantReserva($o_propel->stk_cantReserva);
    $o_BaseImStockFN->setCantDsipon($o_propel->stk_cantDispon);
    $o_BaseImStockFN->setCostoProm($o_propel->stk_costoProm);
    $o_BaseImStockFN->setCostoUlt($o_propel->stk_costoUlt);
    $o_BaseImStockFN->setModifUsr($o_propel->stk_modif_usr);
    $o_BaseImStockFN->setModifFecha($o_propel->stk_modif_fecha);
    
    $o_BaseImStockFN->setItemDescripcion($o_propel->itm_descripcion);
    $o_BaseImStockFN->setAlmacenDescripcion($o_propel->alm_nombre);
    $o_BaseImStockFN->setTipoArticulo($o_propel->itm_tipo);
    $o_BaseImStockFN->setUoM($o_propel->itm_UoM);
    
    return $o_BaseImStockFN;
  }
}